package com.hualan.sfzyproject.controller;


import com.hualan.sfzyproject.domain.PsyQuestion;
import com.hualan.sfzyproject.service.PsyQuestionService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/psyquestion")
public class PsyQuestionController {
    @Autowired
    private PsyQuestionService psyQuestionService;

    @GetMapping("/getAll")
    public R getAll(int order){
        List<PsyQuestion> psyQuestions = psyQuestionService.getAllQuestion(order);
        if (!psyQuestions.isEmpty()){
            return R.ok(psyQuestions);
        }
        return R.error("获取失败");
    }

    @GetMapping("/get")
    public R get(@RequestParam(defaultValue = "1") int pageNum,
                 @RequestParam(defaultValue = "10") int pageSize,int order){
        List<PsyQuestion> psyQuestions = psyQuestionService.getAllQuestion(pageNum, pageSize, order);
        if (!psyQuestions.isEmpty()){
            return R.ok(psyQuestions);
        }
        return R.error("获取失败");
    }

    @GetMapping("/getById")
    public R getById(int questionId){
        PsyQuestion psyQuestion = psyQuestionService.getByQuestionId(questionId);
        if (psyQuestion != null){
            return R.ok(psyQuestion);
        }
        return R.error("获取失败");
    }

    @PostMapping("/insert")
    public R insert(@RequestBody PsyQuestion psyQuestion){
        int i = psyQuestionService.insert(psyQuestion.getTitle(), psyQuestion.getUsers().getUserId(), psyQuestion.getContent());
        if (i > 0){
            return R.ok("插入成功");
        }
        return R.error("插入失败");
    }


    //模糊搜索
    @GetMapping("/selectByLike")
    public R selectByLike(String like){
        return R.ok(psyQuestionService.selectByLike(like));
    }

    @DeleteMapping("{questionId}")
    public R delete(@PathVariable("questionId") int questionId){
        int i = psyQuestionService.delete(questionId);
        if (i > 0){
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

}
